import httpx

api_key = "icoolkj-3d2d71e2a20a4b1a95269db9f3f32c9c"
url = "https://edgetts.api.icoolkj.cn/v1/audio/speech"
headers = {
    "Authorization": f"Bearer {api_key}",  # 注意Bearer后的空格
    "Content-Type": "application/json"
}
data = {
    "model": "tts-1",
    "input": """
        欢迎大家关注旺旺小布丁的小红书！这里会分享超多实用的生活技巧、好物推荐，
        还有各种有趣的日常vlog。希望大家喜欢我的内容，记得点赞收藏加关注哦，
        你们的支持就是我更新的最大动力！后续还会有更多精彩内容，不要错过啦～
    """,
    "voice": "zh-CN-XiaoxiaoNeural",
    "speed": 1.0,
    "response_format": "mp3"
}

try:
    response = httpx.post(url, headers=headers, json=data, timeout=30)
    print(f"状态码: {response.status_code}")
    print(f"响应内容: {response.text}")  # 关键：查看服务器返回的具体原因
    if response.status_code == 200:
        with open("test_edgetts.mp3", "wb") as f:
            f.write(response.content)
        print("音频保存成功")
except Exception as e:
    print(f"请求错误: {e}")